package com.weatherapp.simple;

import android.app.AlertDialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SwitchCompat;

public class SettingsActivity extends AppCompatActivity {

    private ImageView ivBack;
    private LinearLayout llTemperatureUnit;
    private TextView tvTemperatureUnitValue;
    private LinearLayout llAbout;
    
    private SharedPreferences sharedPreferences;
    private boolean isCelsius = true; // true for Celsius, false for Fahrenheit

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        
        initViews();
        initSharedPreferences();
        loadSettings();
        setupClickListeners();
    }

    private void initViews() {
        ivBack = findViewById(R.id.iv_back);
        llTemperatureUnit = findViewById(R.id.ll_temperature_unit);
        tvTemperatureUnitValue = findViewById(R.id.tv_temperature_unit_value);
        llAbout = findViewById(R.id.ll_about);
    }

    private void initSharedPreferences() {
        sharedPreferences = getSharedPreferences("weather_settings", MODE_PRIVATE);
    }

    private void loadSettings() {
        // 加载温度单位设置
        isCelsius = sharedPreferences.getBoolean("is_celsius", true);
        updateTemperatureUnitDisplay();
    }

    private void updateTemperatureUnitDisplay() {
        if (isCelsius) {
            tvTemperatureUnitValue.setText(R.string.celsius);
        } else {
            tvTemperatureUnitValue.setText(R.string.fahrenheit);
        }
    }

    private void setupClickListeners() {
        // 返回按钮
        ivBack.setOnClickListener(v -> finish());
        
        // 温度单位设置
        llTemperatureUnit.setOnClickListener(v -> showTemperatureUnitDialog());
        
        // 关于
        llAbout.setOnClickListener(v -> showAboutDialog());
    }

    private void showTemperatureUnitDialog() {
        String[] units = {getString(R.string.celsius), getString(R.string.fahrenheit)};
        int selectedIndex = isCelsius ? 0 : 1;
        
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(R.string.temperature_unit)
                .setSingleChoiceItems(units, selectedIndex, (dialog, which) -> {
                    isCelsius = (which == 0);
                    updateTemperatureUnitDisplay();
                    sharedPreferences.edit().putBoolean("is_celsius", isCelsius).apply();
                    dialog.dismiss();
                    Toast.makeText(this, "温度单位已更改为 " + units[which], Toast.LENGTH_SHORT).show();
                })
                .setNegativeButton("取消", null)
                .show();
    }

    private void showAboutDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("关于天气预报")
                .setMessage("天气预报 v1.0.0\n\n一个简洁美观的天气预报应用\n\n" +
                           "功能特点：\n" +
                           "• 实时天气信息\n" +
                           "• 逐小时天气预报\n" +
                           "• 未来7天天气预报\n" +
                           "• 多城市管理\n" +
                           "• 个性化设置\n\n" +
                           "© 2024 天气预报")
                .setPositiveButton("确定", null)
                .show();
    }
}